package com.ruoyi.web.controller.train;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.train.domain.JyTrainingSchoolUser;
import com.ruoyi.train.service.IJyTrainingSchoolService;
import com.ruoyi.train.service.IJyTrainingSchoolUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息
 *
 * @author ruoyi
 */
@Slf4j
@RestController
@RequestMapping("/train/user")
@RequiredArgsConstructor
public class JyTrainingSchoolUserController extends BaseController {
    private final IJyTrainingSchoolUserService trainingSchoolUserService;

    /**
     * 获取用户列表
     */
    @GetMapping("/list")
    public TableDataInfo list(JyTrainingSchoolUser user) {
        startPage();
        List<JyTrainingSchoolUser> list = trainingSchoolUserService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 根据用户编号获取详细信息
     */
    @GetMapping(value = {"/", "/{userId}"})
    public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId) {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(userId)) {
            JyTrainingSchoolUser sysUser = trainingSchoolUserService.selectUserById(userId);
            ajax.put(AjaxResult.DATA_TAG, sysUser);
        }
        return ajax;
    }

    /**
     * 修改用户
     */
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody JyTrainingSchoolUser user) {
        user.setUpdateBy(getUsername());
        return toAjax(trainingSchoolUserService.updateUser(user));
    }

    /**
     * 重置密码
     */
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody JyTrainingSchoolUser user) {
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        user.setUpdateBy(getUsername());
        return toAjax(trainingSchoolUserService.resetPwd(user));
    }

    /**
     * 状态修改
     */
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody JyTrainingSchoolUser user) {
        user.setUpdateBy(getUsername());
        return toAjax(trainingSchoolUserService.updateUserStatus(user));
    }
}
